package com.gzstv.dlnasdk.upnp;

import java.net.URL;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Device {
    private String friendlyName;
    private String udn;
    private String location;
    private URL presentationUrl;
    private String ipAddress;
    private final List<Service> services = new CopyOnWriteArrayList<>();

    public Device(String friendlyName, String udn, String location) {
        this.friendlyName = friendlyName;
        this.udn = udn;
        this.location = location;
        try {
            this.ipAddress = new URL(location).getHost();
        } catch (Exception e) {
            this.ipAddress = "";
        }
    }

    public String getFriendlyName() {
        return friendlyName;
    }

    public void setFriendlyName(String friendlyName) {
        this.friendlyName = friendlyName;
    }

    public String getUdn() {
        return udn;
    }

    public String getLocation() {
        return location;
    }

    public URL getPresentationUrl() {
        return presentationUrl;
    }

    public void setPresentationUrl(URL presentationUrl) {
        this.presentationUrl = presentationUrl;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public List<Service> getServices() {
        return services;
    }

    public void addService(Service service) {
        services.add(service);
    }

    public Service getService(String serviceType) {
        for (Service service : services) {
            if (service.getServiceType().contains(serviceType)) {
                return service;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "Device{" +
                "friendlyName='" + friendlyName + "'" +
                ", udn='" + udn + "'" +
                ", location='" + location + "'" +
                ", services=" + services.size() +
                '}';
    }
}